package com.ruoyi.gwc.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SnowFlakeIDGenerator;
import com.ruoyi.gwc.domain.TCostRecord;
import com.ruoyi.gwc.mapper.TCostRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gwc.mapper.TFuelCardManagementMapper;
import com.ruoyi.gwc.domain.TFuelCardManagement;
import com.ruoyi.gwc.service.ITFuelCardManagementService;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 加油卡管理Service业务层处理
 * 
 * @author wp
 * @date 2024-12-13
 */
@Service
public class TFuelCardManagementServiceImpl extends ServiceImpl<TFuelCardManagementMapper, TFuelCardManagement> implements ITFuelCardManagementService
{
    @Autowired
    private TFuelCardManagementMapper tFuelCardManagementMapper;
    @Autowired
    private TCostRecordMapper tCostRecordMapper;

    /**
     * 查询加油卡管理
     * 
     * @param fuelId 加油卡管理主键
     * @return 加油卡管理
     */
    @Override
    public TFuelCardManagement selectTFuelCardManagementByFuelId(Long fuelId)
    {
        return tFuelCardManagementMapper.selectTFuelCardManagementByFuelId(fuelId);
    }

    /**
     * 查询加油卡管理列表
     * 
     * @param tFuelCardManagement 加油卡管理
     * @return 加油卡管理
     */
    @Override
    public List<TFuelCardManagement> selectTFuelCardManagementList(TFuelCardManagement tFuelCardManagement)
    {
        return tFuelCardManagementMapper.selectTFuelCardManagementList(tFuelCardManagement);
    }

    /**
     * 新增加油卡管理
     * 
     * @param tFuelCardManagement 加油卡管理
     * @return 结果
     */
    @Override
    public int insertTFuelCardManagement(TFuelCardManagement tFuelCardManagement)
    {
        Long fuelId = SnowFlakeIDGenerator.getInstance().nextId();
        //新增加油卡购买记录同步增加一条费用记录
        TCostRecord tCostRecord = new TCostRecord();
        tCostRecord.setRecordId(SnowFlakeIDGenerator.getInstance().nextId());
        tCostRecord.setCompanyId(tFuelCardManagement.getCompanyId());
        tCostRecord.setFuelId(fuelId);
        tCostRecord.setVehicleId(tFuelCardManagement.getVehicleId());
        tCostRecord.setCostType(0L);//0收入1支出
        tCostRecord.setCostName(0L);//0加油费1停车费2过路费3维修费4保养费5保险费6其他费用7etc费用
        tCostRecord.setBuyTime(tFuelCardManagement.getBuyTime());
        tCostRecord.setRemarks("加油卡购买记录");
        tCostRecord.setAmount(tFuelCardManagement.getAmount());
        tCostRecord.setCreateTime(DateUtils.getNowDate());
        tCostRecord.setCreateBy(getUsername());
        tCostRecordMapper.insertTCostRecord(tCostRecord);

        tFuelCardManagement.setCreateTime(DateUtils.getNowDate());
        tFuelCardManagement.setCreateBy(getUsername());
        tFuelCardManagement.setFuelId(fuelId);
        return tFuelCardManagementMapper.insertTFuelCardManagement(tFuelCardManagement);
    }

    /**
     * 修改加油卡管理
     * 
     * @param tFuelCardManagement 加油卡管理
     * @return 结果
     */
    @Override
    public int updateTFuelCardManagement(TFuelCardManagement tFuelCardManagement)
    {
        tFuelCardManagement.setUpdateTime(DateUtils.getNowDate());
        tFuelCardManagement.setUpdateBy(getUsername());
        return tFuelCardManagementMapper.updateTFuelCardManagement(tFuelCardManagement);
    }

    /**
     * 批量删除加油卡管理
     * 
     * @param fuelIds 需要删除的加油卡管理主键
     * @return 结果
     */
    @Override
    public int deleteTFuelCardManagementByFuelIds(Long[] fuelIds)
    {
        return tFuelCardManagementMapper.deleteTFuelCardManagementByFuelIds(fuelIds);
    }

    /**
     * 删除加油卡管理信息
     * 
     * @param fuelId 加油卡管理主键
     * @return 结果
     */
    @Override
    public int deleteTFuelCardManagementByFuelId(Long fuelId)
    {
        return tFuelCardManagementMapper.deleteTFuelCardManagementByFuelId(fuelId);
    }
}
